package pl.gigiel.behaviour.steer;

import java.util.List;

import pl.gigiel.behaviour.IBoid;
import pl.gigiel.behaviour.ISteer;
import pl.gigiel.behaviour.exception.BehaviourException;

import com.jme.math.Vector3f;

public class AlignmentSteer implements ISteer {
	private static ISteer steer;

	private AlignmentSteer() {
	}

	public static ISteer getInstance() {
		if (steer == null) {
			steer = new AlignmentSteer();
		}
		return steer;
	}

	@Override
	public void init() throws BehaviourException {
		// nothing
	}

	@Override
	public Vector3f force(IBoid boid, float elapsed) throws BehaviourException {
		Vector3f result = new Vector3f();
		List<IBoid> neighbours = boid.getEnvironment().getNeighbours(boid);
		if (!neighbours.isEmpty()) {
			for (IBoid neighbour : neighbours) {
				result.addLocal(neighbour.getFeatures().getDirection());
			}
			result.divideLocal(neighbours.size());
			result.subtractLocal(boid.getFeatures().getDirection());
		}
		return result;
	}
}
